from codesecurity.interface.obfuscator_js import obfuscate
from codesecurity.interface.jStap import JStapMeta,build_pdg,train_meta
from codesecurity.data.package_extract import iter_dir

import os
import enum

class ObfuscatorMode(enum.Enum):
    default = 'default'
    light = 'light'
    medium = 'medium'
    heavy = 'heavy'

    @staticmethod
    def instance(mode):
        if mode=='default':
            return ObfuscatorMode.default
        elif mode=='light':
            return ObfuscatorMode.light
        elif mode=='medium':
            return ObfuscatorMode.medium
        elif mode=='heavy':
            return ObfuscatorMode.heavy
        else:
            raise ValueError(f"Unknown obfuscator mode: {mode}")

def obfuscate_js(in_dir, out_dir, mode:ObfuscatorMode=ObfuscatorMode.default):
    os.makedirs(out_dir, exist_ok=True)    
    for file in iter_dir(in_dir):
        if file.endswith('.js'):
            out_file=os.path.join(out_dir,file.split('/')[-1])
            obfuscate(file, out_file, mode.value)
        

def jstap_train(meta:JStapMeta,rebuild=False):
    build_pdg(meta.train_bad,rebuild)
    build_pdg(meta.train_good,rebuild)
    build_pdg(meta.validate_good,rebuild)
    train_meta(meta)